﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Gakita.View
{
    class Camera
    {
        private Vector2 _modelCenterPosition        =       new Vector2(0, 0);
        private float _scale                        =       96.0f;

        public float GetScale()
        {
            return _scale;
        }

        public void SetZoom(float aScale)
        {
            _scale = aScale;
        }

        public Vector2 GetViewPosition(float aXPosition, float aYPosition, Vector2 aViewPortSize)
        {
            Vector2 modelPosition = new Vector2(aXPosition, aYPosition);

            Vector2 modelViewPortSize = new Vector2(aViewPortSize.X / _scale, aViewPortSize.Y / _scale);

            //get model top left position
            Vector2 modelTopLeftPosition = _modelCenterPosition - modelViewPortSize / 2.0f;

            return (modelPosition - modelTopLeftPosition) * _scale;
        }

        public Vector2 GetModelPosition(float aXPosition, float aYPosition, Vector2 aViewPortSize)
        {
            Vector2 viewPosition = new Vector2(aXPosition, aYPosition);

            Vector2 modelViewPortSize = new Vector2(aViewPortSize.X / _scale, aViewPortSize.Y / _scale);

            //get model top left position
            Vector2 modelTopLeftPosition = _modelCenterPosition - modelViewPortSize / 2.0f;

            return (viewPosition / _scale) + modelTopLeftPosition;

        }

        public void CenterOn(Vector2 aNewCenterPosition, Vector2 aViewPortSize, Vector2 aLevelSize)
        {
            _modelCenterPosition = aNewCenterPosition;

            Vector2 modelViewPortSize = new Vector2(aViewPortSize.X / _scale, aViewPortSize.Y / _scale);

            //check left
            if (_modelCenterPosition.X < modelViewPortSize.X / 2.0f)
            {
                _modelCenterPosition.X = modelViewPortSize.X / 2.0f;
            }

            //check bottom
            if (_modelCenterPosition.Y > aLevelSize.Y - modelViewPortSize.Y / 2.0f)
            {
                _modelCenterPosition.Y = aLevelSize.Y - modelViewPortSize.Y / 2.0f;
            }

            //check top
            if (_modelCenterPosition.Y < modelViewPortSize.Y / 2.0f)
            {
                _modelCenterPosition.Y = modelViewPortSize.Y / 2.0f;
            }

            //check right
            if (_modelCenterPosition.X > aLevelSize.X - modelViewPortSize.X / 2.0f)
            {
                _modelCenterPosition.X = aLevelSize.X - modelViewPortSize.X / 2.0f;
            }
        }
        public void BottomOn(Vector2 aNewBottomPosition, Vector2 aViewPortSize, Vector2 aLevelSize)
        {
            Vector2 modelViewPortSize = new Vector2(aViewPortSize.X / _scale, aViewPortSize.Y / _scale);
            Vector2 _centerPosition = aNewBottomPosition;
            _centerPosition.Y -= modelViewPortSize.Y/2;
            _modelCenterPosition = _centerPosition;

            //check left
            if (_modelCenterPosition.X < modelViewPortSize.X / 2.0f)
            {
                _modelCenterPosition.X = modelViewPortSize.X / 2.0f;
            }

            //check bottom
            if (_modelCenterPosition.Y > aLevelSize.Y - modelViewPortSize.Y / 2.0f)
            {
                _modelCenterPosition.Y = aLevelSize.Y - modelViewPortSize.Y / 2.0f;
            }

            //check top
            if (_modelCenterPosition.Y < modelViewPortSize.Y / 2.0f)
            {
                _modelCenterPosition.Y = modelViewPortSize.Y / 2.0f;
            }

            //check right
            if (_modelCenterPosition.X > aLevelSize.X - modelViewPortSize.X / 2.0f)
            {
                _modelCenterPosition.X = aLevelSize.X - modelViewPortSize.X / 2.0f;
            }
        }
    }
}
